package com.gjzt.business.publish.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.servlet.RequestAttribute;
import com.eastdigit.servlet.Response;
import com.eastdigit.system.model.Constants;
import com.eastdigit.system.model.Role;
import com.eastdigit.system.service.RoleService;
import com.gjzt.business.publish.service.PublishBillAuditService;
import com.gjzt.business.publish.service.PublishBillService;

@Controller
@RequestMapping(value = "/publishBill")
public class PublishBillController {

    @Autowired
    private PublishBillService publishBillService;
    @Autowired
    private PublishBillAuditService publishBillAuditService;
    
    @Autowired
    private RoleService roleService;
    
    private List<String> getRoleIdListByUser(String userId){
        List<String> roleIds = null;
        List<Role> roles = roleService.queryRoleOfUser(userId);
        
        if(roles != null && !roles.isEmpty()){
            roleIds = new ArrayList<String>();
            for(Role role : roles){
                String roleId = role.getId();
                roleIds.add(roleId);
            }
        }
        return roleIds;
    }
    
    @RequestMapping(value = "/tasks")
    @ResponseBody
    public JSONObject queryTenderbidAuditTask(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        if(!Constants.SUPER_USER_ID.equals(reqBean.getUserId())){
            reqBean.addInfo("candidateOrAssignedUserId", reqBean.getUserId());
            reqBean.addInfo("candidateGroups", getRoleIdListByUser(reqBean.getUserId()));
        }
        return Response.response(publishBillAuditService.getTasks(reqBean));
    }

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject queryAdPublishBill(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(publishBillService.findAdPublishBill(reqBean));
    }
    
    @RequestMapping(value = "/{billId}/info")
    @ResponseBody
    public JSONObject busOrStopHead(@PathVariable String billId) {
        return Response.response(publishBillService.getPublishBill(billId).toJsonObject());
    }


    @RequestMapping(value = "/queryBillDetails")
    @ResponseBody
    public JSONObject queryBillDetails(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(publishBillService.queryBillDetails(reqBean.getInfo("billId")));
    }

    /**
     * 制作单明细，车辆站点表头
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/busOrStopHead")
    @ResponseBody
    public JSONObject busOrStopHead(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(publishBillService.busOrStopHead(reqBean));
    }
    
    /**
     * 制作单
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "{billId}/makeBills")
    @ResponseBody
    public JSONObject queryMakeBillDetails(@PathVariable String billId) {
        return Response.response(publishBillService.queryMakeBills(billId));
    }
    
    


    /**
     * 添加
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject addAdPublishBill(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        publishBillService.addAdPublishBill(reqBean);
        return Response.responseSuccess();
    }


    /**
     * 添加上刊单
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/saveBillDetails")
    @ResponseBody
    public JSONObject saveBillDetails(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response( publishBillService.saveBillDetails(reqBean));
    }

    /**
     * 修改
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject updateAdPublishBill(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        publishBillService.updateAdPublishBill(reqBean);
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/startPublishBill")
    @ResponseBody
    public JSONObject startPublishBill(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	publishBillAuditService.startPublishBill(reqBean);
        return Response.responseSuccess();
    }


    /**
     * 删除
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject removeAdPublishBill(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        publishBillService.removeAdPublishBill(reqBean);
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/completeBill")
    @ResponseBody
    public JSONObject completeBill(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        publishBillService.completeBill(reqBean);
        return Response.responseSuccess();
    }


    @RequestMapping(value = "/downLoadFile")
    @ResponseBody
    public JSONObject downLoadFile(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean, HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {
        publishBillService.downLoadFile(reqBean, request, response);
        return Response.responseSuccess();
    }



}